package com.snacks;

import java.util.List;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.snacks.map.MyPlaceRoom;

public class MapPost extends MapActivity implements LocationListener, OnClickListener{
	
	MapView mapView = null;
	MapController mapController;
	private MapController mc;
	private GeoPoint geoPoint, p, point;
	private MyPlaceRoom myPlaceRoom;
	private Button btDroppin, btOk;
	private List<Overlay> list;
	private LocationManager locationManager;
	private Location location;

	private Bitmap bmp;

	int iLongitude, iLatitude;
	private boolean kt = false;
	
	double dlongitude = 21.005997 * 1E6;
	double dlatitude = 105.805396 * 1E6;
	
	@Override
	protected void onCreate(Bundle icicle) {
		// TODO Auto-generated method stub
		super.onCreate(icicle);
		setContentView(R.layout.map_post);
		
		mapView = (MapView) findViewById(R.id.mapView);

		bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pushpin);
		btDroppin = (Button) findViewById(R.id.map_btdroppin);
		btDroppin.setOnClickListener(this);
		
		btOk = (Button) findViewById(R.id.map_btok);
		btOk.setOnClickListener(this);
		
		LinearLayout zoomLayout = (LinearLayout) findViewById(R.id.zoom);
		View zoomView = mapView.getZoomControls();

		zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

		// Hien thi bo dieu khien zoom
		mapView.displayZoomControls(true);
		mc = mapView.getController();

		p = new GeoPoint((int) (dlongitude), (int) (dlatitude));
		
		mc.animateTo(p);
		mc.setZoom(15);
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	public void onLocationChanged(Location location) {
		point = new GeoPoint((int) (location.getLatitude() * 1E6),
				(int) (location.getLongitude() * 1E6));
		myGPS mGps = new myGPS(bmp, point);
		list = mapView.getOverlays();
		list.add(mGps);
		mapController.animateTo(point);
		mapController.setZoom(17);
		mapView.invalidate();
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.map_btdroppin:

			kt = true;
			String[] toadoHaNoi = { "21.020000", "105.520000" };
			Double lati = Double.parseDouble(toadoHaNoi[0]);
			Double longi = Double.parseDouble(toadoHaNoi[1]);
			geoPoint = new GeoPoint((int) (lati * 1e6), (int) (longi * 1E6));
			myPlaceRoom = new MyPlaceRoom(bmp, geoPoint);
			list = mapView.getOverlays();
			list.add(myPlaceRoom);
			mapView.invalidate();

			break;

		case R.id.map_btok:
			if (kt == true) {
				iLongitude = MyPlaceRoom.geoPoint.getLatitudeE6();
				iLatitude = MyPlaceRoom.geoPoint.getLongitudeE6();
				String point1 = iLongitude + ";" + iLatitude;
				String diem = point1.substring(0, point1.indexOf(";") - 6)
						+ "."
						+ point1.substring(point1.indexOf(";") - 6,
								point1.indexOf(";") + 1)
						+ point1.substring(point1.indexOf(";") + 1,
								point1.length() - 6)
						+ "."
						+ point1.substring(point1.length() - 6, point1.length());
				Intent i = new Intent(getBaseContext(), Post.class);
				Bundle bundle = new Bundle();
				bundle.putString("Toado", diem);
				i.putExtras(bundle);
				startActivity(i);
				kt = false;
			} else {
				Intent ii = new Intent(getBaseContext(), Post.class);
				startActivity(ii);
			}
			break;
		default:
			break;
		}
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}
	
	public class myGPS extends Overlay {
		private Bitmap bmp;
		private GeoPoint geoPoint;
		

		public myGPS(Bitmap bmp, GeoPoint geoPoint) {
			this.bmp = bmp;
			this.geoPoint = geoPoint;
		}

		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			shadow = false;
			Point point = new Point();
			mapView.getProjection().toPixels(geoPoint, point);
			canvas.drawBitmap(bmp, point.x, point.y, null);
			super.draw(canvas, mapView, shadow);
		}
		@Override
		public boolean onTap(GeoPoint p, MapView mapView) {
			return super.onTap(p, mapView);
		}
	}

}
